﻿using MindASPCoreWebAPI.DtoModel.SystemManager;
using MindASPCoreWebAPI.IRepository.SystemManager;
using MindASPCoreWebAPI.PoModel.SystemManager;
using MindASPCoreWebAPI.Repository.CommonLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MindASPCoreWebAPI.Repository.SystemManager
{
    //字典（DAL）
   public class DictionariesRepository: BaseRepository, IDictionariesRepository
    {

        // 上下文       
        public DictionariesRepository(SystemCenterMindContext systemcentermindContext) : base(systemcentermindContext)
        {
        }


        /// <summary>
        /// 根据字典类别key获取下面的字典选项
        /// </summary>
        /// <param name="typeKey">字典类别key</param>
        /// <returns></returns>
        public List<Dictionary_Model> GetDtListByTypeKey(string typeKey)
        {
            return _systemCenterDbSoaContext.Dictionary_Model.Where(c => c.dt_type_key == typeKey).ToList();
        }


        //字典添加
        public int Add_Dictionaries(DictionariesModel dictionariesModel )
        {
            _systemCenterDbSoaContext.DictionariesModel.Add(dictionariesModel);
            List<DictionariesModel> dic = new List<DictionariesModel>();
            dic.Add(new DictionariesModel()
            {
                admin_id = 1,
                dic_createdate=DateTime.Now
            }) ;

            return _systemCenterDbSoaContext.SaveChanges();
                
            
        }

        //字典显示
        public List<DictionariesDto> Show_Dictionaries(out int totalcount,int pageindex,int pagesize,int? dic_status,string dic_type="",string dic_name="")
        {
            var sql = from a in _systemCenterDbSoaContext.DictionariesModel
                      join b in _systemCenterDbSoaContext.AdminModel on a.admin_id equals b.admin_id
                      select new DictionariesDto
                      {
                          admin_id = a.admin_id,
                          admin_name = b.admin_name,
                          dic_createdate = a.dic_createdate,
                          dic_id = a.dic_id,
                          dic_name = a.dic_name,
                          dic_order = a.dic_order,
                          dic_status = a.dic_status,
                          dic_type = a.dic_type
                      };
            if(!string.IsNullOrWhiteSpace(dic_name))
            {
                sql = sql.Where(a => a.dic_name.Contains(dic_name));
            }
            if(dic_status!=null)
            {
                sql = sql.Where(a => a.dic_status == dic_status);
            }
            if(dic_type!=null)
            {
                sql = sql.Where(a => a.dic_type == dic_type);
            }
            totalcount = sql.Count();
            return sql.OrderBy(a => a.dic_id).Skip((pageindex - 1) * pagesize).Take(pagesize).ToList();



        }

        //编辑
        public DictionariesModel Eait_DictionariesModel(int dic_id)
        {
            return _systemCenterDbSoaContext.DictionariesModel.FirstOrDefault(a => a.dic_id == dic_id);
        }

        //修改
        

          



    }
}
